<?php
$deal_id = $params->tbl_row_id;
$deal_name = $columns['name']['value'];
$slug = $columns['camp_link']['value'];

/* Points Part new Deal */ 
if($params->is_new_pod_item == true){

  if($columns['sale']['value'] == 1){
    
    $user_id = $columns['added_by_user']['value'];
    
    $points = new Pod('points');
    $points->page = 1;
    $points->search = false;
    $points->findRecords('id DESC', "1", "user.id = '$user_id'");
    while($points->fetchRecord())
    {
      $points_id = $points->get_field('id');
      $deals_points = $points->get_field('deals');
      $user_name = $points->get_field('user.name');
      $user_slug = $points->get_field('user.slug');
    }
    
    if(!empty($columns['image']['value'])){
      $columns_points['deals'] = ($deals_points + 500);
    }
    else
    {
      $columns_points['deals'] = ($deals_points + 300);
    }
    
    $api = new PodAPI();
    $params_points = array('datatype' => 'points', 'tbl_row_id' => $points_id, 'columns' => $columns_points);
    $api->save_pod_item($params_points);
  
    $msg=
      '<div id="body" style="width: 500px; border: solid; border-color: #FFFFFF; background-color: #FFFFFF;">
      <div id="logo" style="text-align: left">
      	<img style="margin-left: 178px; margin-top: 10px;" height="40" src="http://buzzr.in/wp-content/themes/buzzr_960/img/buzzrin-logo.png" />
      </div>
      <div id="content" style="width: auto; margin: 20px; padding: 5px; background-color: #D0F0FB; font-size: 14px; color:#1BA3DF;">
      Hi,<br /><br />
      <i><b>Chetan</b></i><br /><br />
      A New Sale has been added by <a href="'.get_bloginfo('url').'/member/'.$user_slug.'">'.$user_name.'</a><br /><br />
      Details of the Sale:<br /><b>Viewed at: </b><a href="'.get_bloginfo('url').'/deals/'.sanitize_title($deal_name).'">'.$deal_name.'</a><br />
      <a href="'.get_bloginfo('url').'/edit/'.sanitize_title($deal_name).'"><b>Edit it</b></a><br /><br />
      Expiry Date: '.$columns['camp_end']['value'].'<br />
      Thank You!
      </div>'; 
    
    $headers = array("From: Buzzr.in <info@enthrallmedia.com>","Content-Type: text/html");
    $h = implode("\r\n",$headers) . "\r\n";
    wp_mail('chetan.bhatia@enthrallmedia.com', 'New Sale has been Added!', $msg, $h);
  }
  
}

/* Points Part new Deal */



/* Brand Part for New Deal */
if($params->is_new_pod_item == true){
$brand = pods_sanitize($columns['brand_name_pf']['value']);

if(!empty($brand))
{
  $columns_brand['name'] = $brand;
  $columns_brand['brandurl'] = sanitize_title($columns_brand['name']); 
  
  $brand = new Pod('brand');
  $brand->page = 1;
  $brand->search = false;
  $where = "t.brandurl = '".$columns_brand['brandurl']."'";
  $brand->findRecords('id DESC', -1,$where);
  $brandFound = $brand->getTotalRows();
  
  if($brandFound != 1)
  {
    $api_brand = new PodAPI();
    $columns_brand['local'] = '1';
    $params_brand = array('datatype' => 'brand', 'columns' => $columns_brand);
    $brand_id = $api_brand->save_pod_item($params_brand);
  }
  else
  {
    while($brand->fetchRecord())
    {
      $brand_id = $brand->get_field('id');
    }
  }
  
  $api_deal_track = new PodAPI();
  $columns_deal['name'] = $deal_name;
  $columns_deal['brand'] = $brand_id;
  $params_deal = array('datatype' => 'deal', 'tbl_row_id' => $deal_id, 'columns' => $columns_deal);
  $api_deal_track->save_pod_item($params_deal);
}
}
/* Brand Part for New Deal */

/* Tags Part Add & Edit */
$tags = pods_sanitize($columns['tags']['value']);

if(!empty($tags))
{
  function trim_value(&$value) 
  { 
    $value = trim($value); 
  }
  $tags_array = explode(",",$tags);
  array_walk($tags_array, 'trim_value');

	$tags_pod = new Pod('tags');
	$tags_pod->page = 1;
	$tags_pod->search = false;
	$tags_pod->findRecords('name ASC', -1);
	while($tags_pod->fetchRecord())
	{
  	$name  = $tags_pod->get_field('name');
  	$current_tags[] = $name;
	}
	
  if(count($current_tags) != 0)
  {
    $unique_tags = array_diff($tags_array, $current_tags);
	}
	else
	{
    $unique_tags = $tags_array;
  }
  $unique_tags = array_values($unique_tags);
  $count_ut = count($unique_tags);

	$repeated_tags = array_intersect($tags_array, $current_tags);
	$repeated_tags = array_values($repeated_tags);
  $count_rt = count($repeated_tags);

	if($count_rt != 0)
	{ 
		$k = 0;
  
    foreach($repeated_tags as $val)
    {
      $tags_string.= "'".$val."'";
  		if($k != ($count_rt-1))
  		{
  			$tags_string.= ",";
  		}
  		$k++;
    }
    $repeated_tags = (string)$tags_string;
    
    $tags_pod_rep = new Pod('tags');
    $tags_pod_rep->page = 1;
	  $tags_pod_rep->search = false;
    $tags_pod_rep->findRecords("t.id DESC", -1, "t.name IN(".$repeated_tags.")");
		while($tags_pod_rep->fetchRecord())
		{
			$tag_id = $tags_pod_rep->get_field('id');
      $campaign_ids[$tag_id]  = $tags_pod_rep->get_field('campaigns.id');
      $campaign_ids[$tag_id][] = $deal_id;
      
			$campaign_ids[$tag_id] = array_unique($campaign_ids[$tag_id]);
		}
 
    foreach($campaign_ids as $id_tag => $ids_tag_campaign)
		{
			$tags_campaigns[$id_tag] = implode(',',$ids_tag_campaign);
		}
		
    $api_rt = new PodAPI();
    foreach ($tags_campaigns as $tags_id => $tags_campaign_ids)
		{
			//$sql = "SELECT p.id FROM wp_pod p INNER JOIN wp_pod_types t ON t.id = p.datatype WHERE p.tbl_row_id = '".$tags_id."' AND t.name = 'tags' LIMIT 1";
      //$result = pod_query($sql);
      //$pod_id = mysql_fetch_assoc($result);
      
      $params_rt = array('datatype' => 'tags', 'tbl_row_id' => $tags_id, 'columns' => array('campaigns'=>$tags_campaign_ids));
			$api_rt->save_pod_item($params_rt);
		}
	}
	
	if($count_ut != 0)
	{
		$api_ut = new PodAPI();		
		foreach ($unique_tags as $tag_value)
		{
			$tag_url = "t-".$tag_value;
			$columns_new_tags['name'] = $tag_value;
			$columns_new_tags['slug'] = $tag_url;
			$columns_new_tags['campaigns'] = $deal_id;
			$params_ut = array('datatype' => 'tags', 'columns' => $columns_new_tags);
			$api_ut->save_pod_item($params_ut);
		}
		
		//$api_deal = new PodAPI();
    //$params_deal = array('datatype' => 'deal', 'tbl_row_id' => $deal_id, 'columns' => array('name'=>$deal_name,'tags'=>''));
    //$api_deal->save_pod_item($params_deal);
	}
	
  //$sql_deal = "SELECT id FROM wp_pod WHERE tbl_row_id = '".$deal_id."' LIMIT 1";
  //$result_deal = pod_query($sql_deal);
  //$pod_id_deal = mysql_fetch_assoc($result_deal);


  //$api_deal = new PodAPI();
  //$params_deal = array('datatype' => 'deal', 'tbl_row_id' => $deal_id, 'columns' => array('tags'=>''));
  //$api_deal->save_pod_item($params_deal);
}
/* Tags Part Add & Edit */

?>